package com.server.llh.llhserverwork.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageButton;
import android.widget.TextView;

import com.llh.llhsuppliers.R;


/**
 * Created by ZTF on 2017-06-05.
 */

public class WebViewActivity  extends Activity implements View.OnClickListener {
    private WebView mWebView;
    private TextView titlebar_text;
    private ImageButton left_btn;
    private Bundle mBundle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        setFullScreen(false);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

        mBundle = getIntent().getExtras();
        initView();

    }

    public void initView() {

        left_btn = (ImageButton) findViewById(R.id.main_back);
        left_btn.setOnClickListener(this);
        titlebar_text = (TextView) findViewById(R.id.titleName);
        titlebar_text.setText("乐老新知");
        mWebView = (WebView) findViewById(R.id.web_webview);

        mWebView.loadUrl(mBundle.getString("url"));
        mWebView.requestFocusFromTouch();
        mWebView.getSettings()
                .setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        /** 判断加载过程 */
        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    // 网页加载完成

                } else {
                    // 加载中

                }
            }
        });
        initListener();
    }

    public void initListener() {
        // TODO Auto-generated method stub
        /** 加载javascript */
        // WebSettings mWebSetting = mWebView.getSettings();
        // mWebSetting.setJavaScriptEnabled(true);
        // mWebView.addJavascriptInterface(new Object() {
        // public void clickOnAndroid() {
        // new Handler().post(new Runnable() {
        // public void run() {
        // mWebView.loadUrl("javascript:wave()");
        // }
        // });
        // }
        // }, "demo");

        /** 打开页面时， 自适应屏幕 */
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setUseWideViewPort(true);// 设置此属性，可任意比例缩放
        webSettings.setLoadWithOverviewMode(true);

        /** 便页面支持缩放 */
        webSettings.setJavaScriptEnabled(true);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setSupportZoom(true);

    }

    @Override
    public void onClick(View v) {
        onBackPressed();
    }

    public void setFullScreen(boolean fullScreen) {
        if (fullScreen) {
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
        } else {
            requestWindowFeature(Window.FEATURE_NO_TITLE);
        }
    }
}
